package com.bezdelnick.forum.web.panel;

import com.bezdelnick.forum.util.PropertyUtils;
import com.bezdelnick.forum.web.resource.ImageFromFileSystemReference;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.file.Folder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 03.09.12
 * Time: 13:50
 * To change this template use File | Settings | File Templates.
 */
public class ShowUploadedFilesPanel extends Panel {

    private List<String> files = new ArrayList<String>();
    private Folder repository;
    private String linkToFile = "----------";

    public ShowUploadedFilesPanel(String id) {
        super(id);

        final WebMarkupContainer container = new WebMarkupContainer("container");
        container.setOutputMarkupId(true);
        add(container);

        Label lblSelectedImageName = new Label("file_link", new PropertyModel<String>(this, "linkToFile"));
        container.add(lblSelectedImageName);

        this.files = this.loadFiles();
        ListView filesList = new ListView("row", new PropertyModel(this, "files")) {
            int count = 0;

            @Override
            protected void populateItem(ListItem listItem) {
                final String imageName = (String) listItem.getModelObject();
                final String imagePathAndName = getUploadFolder() + imageName;

                listItem.add(new Label("cell_1", imagePathAndName));

                final Image image = new Image("cell_2", new ImageFromFileSystemReference("wicket_img_" + imageName, imagePathAndName));
                count++;
                image.add(new AjaxEventBehavior("onclick") {
                    @Override
                    protected void onEvent(AjaxRequestTarget target) {
                        linkToFile = imagePathAndName;
                        target.add(container);
                    }
                });
                listItem.add(image);
            }
        };
        add(filesList);
    }

    private List<String> loadFiles() {
        List<String> listPathFiles = new ArrayList<String>();
        File[] files = new Folder(getUploadFolder()).getFiles();
        for (File file : files) {
            String path = file.getName();
            listPathFiles.add(path);
        }
        return listPathFiles;
    }

    private String getUploadFolder() {
        Properties properties = PropertyUtils.loadFromClassPath("application.properties", false);
        String location = properties.getProperty("repository.location");
        return location;
    }
}
